package com.jyxd.web.service.assessment.basic;

import com.alibaba.fastjson.JSON;
import com.jyxd.web.dao.basic.CustomContentDao;
import com.jyxd.web.dao.basic.CustomFieldDao;
import com.jyxd.web.data.basic.CustomContent;
import com.jyxd.web.data.basic.CustomField;
import com.jyxd.web.data.dto.HttpResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Service
@Transactional
public class CustomFieldService {

    @Autowired
    private CustomFieldDao customFieldDao;
    @Autowired
    private CustomContentDao customContentDao;

    public boolean insert(CustomField customField) {
        return customFieldDao.insert(customField);
    }

    public boolean update(CustomField customField) {
        return customFieldDao.update(customField);
    }

    public CustomField queryData(String id) {
        return customFieldDao.queryData(id);
    }

    public List<CustomField> queryList(Map<String, Object> map) {
        return customFieldDao.queryList(map);
    }

    public int queryNum(Map<String, Object> map) {
        return customFieldDao.queryNum(map);
    }

    public int getCustomFieldListCount(Map<String, Object> map) {
        return customFieldDao.getCustomFieldListCount(map);
    }

    public HttpResult deleteByPatientAndAssociatedTable(Map<String, Object> map) {
        errorIfEmpty(map, "参数不能为空");
        errorIfEmpty(map.get("patientId"), "患者id不能为空");
        errorIfEmpty(map.get("associatedTable"), "关联表不能为空");
        List<CustomContent> customContentList = customContentDao.getByAssociatedTableId(map);
        System.out.println(JSON.toJSONString(customContentList));
        System.out.println("---------------------------------------------");
        if (!CollectionUtils.isEmpty(customContentList))
            return new HttpResult(500, "请将历史数据内容清空后再次重置");
        if (customFieldDao.updateDataByPatientAndAssociatedTable(map))
            return new HttpResult(200, "数据删除成功");
        else
            return new HttpResult(500, "数据删除失败");
    }

    /**
     * 查询是否存在历史记录
     *
     * @return
     */
    public HttpResult findCustomHistory(List<CustomField> customFieldList) {
        if (CollectionUtils.isEmpty(customFieldList))
            return new HttpResult(200, "参数数据不存在");
        Map<String, Object> map = new HashMap<>();
        customFieldList.forEach(customField -> {
            map.put("patientId", customField.getPatientId());
            map.put("associatedTable", customField.getAssociatedTable());
            String contentNum = customField.getFieldChName();
            if (StringUtils.contains(contentNum, "tube_")) {
                contentNum = contentNum.replace("tube_", "");
            }
            map.put("contentNum", contentNum);
            List<CustomContent> customContentList = customContentDao.getByAssociatedTableId(map);
            if (CollectionUtils.isEmpty(customContentList))
                customField.setReadOnlyField(true);
            else
                customField.setReadOnlyField(false);
        });
        return new HttpResult(200, customFieldList, "查询成功");
    }
}
